﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections;
using System.Collections.Generic;

/// <summary>
///webs 的摘要说明
/// </summary>
public class webs
{
    private string filename;
    private XElement root;
    private int maxid;
    private int update;
    private IEnumerable<XElement> element;
    
    public  List<web> mywebs = new List<web>();    

    public string Filename
    {
        get
        {
            return this.filename;
        }
        set
        {
            this.filename = value;
        }
    }

    public XElement Root
    {
        set
        {
            root = value;
        }
        get
        {
            return root;
        }
    }

   

    public void fillWebs()
    {
        mywebs = root.Elements("web").Select(one => new web(one)).ToList();
            //from one in root.Elements("web")
            //     select new web(one);
    }

    public bool delete(string idstr)
    {
        try
        {
            XElement xe = root.Elements("web").Where(one => one.Element("ID").Value == idstr).First();
            xe.Remove();
            int tmpid = xe.Elements("web").Max(one => Convert.ToInt32(one.Element("ID").Value));
            root.Attribute("maxID").Value = tmpid.ToString();
            root.Save(filename);
            return true;
        }
        catch (Exception ex)
        {
            LOG.writer(ex.ToString());
            return false;
        }
    }

    public bool modify(string idstr, XElement v)
    {
        try
        {
            XElement xe = root.Elements("web").Where(one => one.Element("ID").Value == idstr).First();
            xe.ReplaceWith(v);
            root.Save(filename);
            return true;
        }
        catch (Exception ex)
        {
            LOG.writer(ex.ToString());
            return false;
        }

    }

    /*
    <web>
    <ID>1</ID>
    <name>search Engine</name>
    <link>www.yahoo.com</link>
    <note>the most famous video web</note>
    <cate>video</cate>
    <!-- cate is category -->
    <cr>99</cr>
    <!-- cr is creater -->
    <key>search,entertainment,</key>
    <!--key is keyword -->
    <lan>English</lan>
    <lan>Jepanese</lan>
    <!-- lan is language -->
    </web>
     * 
     * modify("1","<ID>1</ID><name>search</name>
    <link>www.yahoo.com</link>
    <note>the most famous video web</note>
    <cate>video</cate>
    <!-- cate is category -->
    <cr>99</cr>
    <!-- cr is creater -->
    <key>search,entertainment,</key>
    <!--key is keyword -->
    <lan>English</lan>
    <lan>Jepanese</lan>")
     * 
     * 
     * ---->
     * 
     * 
    <web>
    <ID>1</ID><name>search</name>
    <link>www.yahoo.com</link>
    <note>the most famous video web</note>
    <cate>video</cate>
    <!-- cate is category -->
    <cr>99</cr>
    <!-- cr is creater -->
    <key>search,entertainment,</key>
    <!--key is keyword -->
    <lan>English</lan>
    <lan>Jepanese</lan>
    </web>
     */

    public bool modify(string idstr, string v)
    {
        try
        {
            XElement moXe = XElement.Parse(v);
            XElement xe = root.Elements("web").Where(one => one.Element("ID").Value == idstr).First();
            xe.ReplaceAll(moXe);
            root.Save(filename);
            return true;
        }
        catch (Exception ex)
        {
            LOG.writer(ex.ToString());
            return false;
        }
    }

    public bool add(XElement v)
    {
        try
        {
            int tmpid = Convert.ToInt32(root.Attribute("maxID").Value);
            tmpid++;
            root.Attribute("maxID").Value = tmpid.ToString();
            root.LastNode.AddAfterSelf(v);
            root.Save(filename);
            return true;
        }
        catch (Exception ex)
        {
            LOG.writer(ex.ToString());
            return false;
        }
    }


    public bool add(string v)
    {
        try
        {
            XElement addXe = XElement.Parse(v);
            int tmpid = Convert.ToInt32(root.Attribute("maxID").Value);
            tmpid++;
            root.Attribute("maxID").Value = tmpid.ToString();
            root.LastNode.AddAfterSelf(v);
            root.Save(filename);
            return true;
        }
        catch (Exception ex)
        {
            LOG.writer(ex.ToString());
            return false;
        }
    }

    public string GetRootAttributeValue(string name)
    {
        return root.Attribute(name).Value;
    }

    public bool SetRootRootAttributeValue(string name, string v)
    {
        root.Attribute(name).Value = v;
        root.Save(filename);
        return true;
    }

    //public IEnumerable<web> getWebsByCate(string v)
    //{
    //    return mywebs.Where(el => el.Cate == v);        
    //}

    public webs()
    {

    }

	public webs(string filename)
	{
        this.filename = filename;
        root = XElement.Load(filename);
        mywebs = root.Elements("web").Select(one => new web(one)).ToList();
            //from one in root.Elements("web")
            //     select new web(one);      
	}

    public bool swapWebs(string idA, string idB)
    {
        web _webA = (from el in mywebs
                    where Convert.ToString(el.ID) == idA
                    select el).First();

        return true;
    }

    public web getWebByID(string id)
    {
        foreach (web el in mywebs)
        {
            if (el.ID.ToString() == id)
            {

                return el;
            }
        }
        return null;
    }

    public void addWeb(web parWeb)
    {
        mywebs.Add(parWeb);
    }
}
